package com.ruoyi.merchant.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.merchant.domain.ImsVehViolation;
import com.ruoyi.merchant.domain.dto.ImsVehViolationDto;
import com.ruoyi.merchant.mapper.ImsVehViolationMapper;
import com.ruoyi.merchant.service.IImsVehViolationService;
import com.ruoyi.order.domain.WxVhiceodr;
import com.ruoyi.order.service.IWxVhiceodrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 车辆违章信息Service业务层处理
 * 
 * @author chenyuhang
 * @date 2024-09-19
 */
@Service
public class ImsVehViolationServiceImpl implements IImsVehViolationService 
{
    @Autowired
    private ImsVehViolationMapper imsVehViolationMapper;
    @Autowired
    private IWxVhiceodrService wxVhiceodrService;
    /**
     * 查询车辆违章信息
     * 
     * @param id 车辆违章信息主键
     * @return 车辆违章信息
     */
    @Override
    public ImsVehViolation selectImsVehViolationById(Long id)
    {
        return imsVehViolationMapper.selectImsVehViolationById(id);
    }

    /**
     * 查询车辆违章信息列表
     * 
     * @param imsVehViolation 车辆违章信息
     * @return 车辆违章信息
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<ImsVehViolation> selectImsVehViolationList(ImsVehViolation imsVehViolation)
    {
        return imsVehViolationMapper.selectImsVehViolationList(imsVehViolation);
    }

    /**
     * 新增车辆违章信息
     * 
     * @param imsVehViolation 车辆违章信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertImsVehViolation(ImsVehViolation imsVehViolation)
    {
        // 修改订单状态，将未登记违章改为已登机违章
        WxVhiceodr wxVhiceodr = new WxVhiceodr();
        wxVhiceodr.setId(imsVehViolation.getOrderId());
        wxVhiceodr.setIsRecord(true);
        if (wxVhiceodrService.updateWxVhiceodr(wxVhiceodr) == 0) {
            return 0;
        }
        LoginUser loginUser= SecurityUtils.getLoginUser();
        imsVehViolation.setCreateTime(DateUtils.getNowDate());
        imsVehViolation.setEntryName(loginUser.getUsername());
        imsVehViolation.setEntryUserId(loginUser.getUserId());
        if (imsVehViolation.getStatus()==2){
            imsVehViolation.setConfirmName(loginUser.getUsername());
            imsVehViolation.setConfirmUserId(loginUser.getUserId());
        }
        return imsVehViolationMapper.insertImsVehViolation(imsVehViolation);
    }

    /**
     * 修改车辆违章信息
     * 
     * @param imsVehViolation 车辆违章信息
     * @return 结果
     */
    @Override
    public int updateImsVehViolation(ImsVehViolation imsVehViolation)
    {
        LoginUser loginUser= SecurityUtils.getLoginUser();
        if (imsVehViolation.getStatus()==2){
            imsVehViolation.setConfirmName(loginUser.getUsername());
            imsVehViolation.setConfirmUserId(loginUser.getUserId());
        }
        imsVehViolation.setUpdateBy(loginUser.getUsername());
        imsVehViolation.setUpdateTime(DateUtils.getNowDate());
        return imsVehViolationMapper.updateImsVehViolation(imsVehViolation);
    }

    /**
     * 批量删除车辆违章信息
     * 
     * @param ids 需要删除的车辆违章信息主键
     * @return 结果
     */
    @Override
    public int deleteImsVehViolationByIds(Long[] ids)
    {
        return imsVehViolationMapper.deleteImsVehViolationByIds(ids);
    }

    /**
     * 删除车辆违章信息信息
     * 
     * @param id 车辆违章信息主键
     * @return 结果
     */
    @Override
    public int deleteImsVehViolationById(Long id)
    {
        return imsVehViolationMapper.deleteImsVehViolationById(id);
    }

    @Override
    public int generateVehViolation(WxVhiceodr wxVhiceodr) {
        ImsVehViolation imsVehViolation = new ImsVehViolation();
        imsVehViolation.setCarNum(wxVhiceodr.getCarNum());
        imsVehViolation.setVehicleId(wxVhiceodr.getCid());
        imsVehViolation.setOrderId(wxVhiceodr.getId());
        imsVehViolation.setOrderNum(wxVhiceodr.getOrderNum());
        imsVehViolation.setStatus(1);
        imsVehViolation.setDeptId(wxVhiceodr.getDeptId());
        imsVehViolation.setWechatUserId(wxVhiceodr.getWid());
        imsVehViolation.setCreateTime(DateUtils.getNowDate());
        return imsVehViolationMapper.insertImsVehViolation(imsVehViolation);
    }

    @Override
    public int selectVioQuantity(ImsVehViolationDto imsVehViolation) {
        return imsVehViolationMapper.selectVioQuantity(imsVehViolation);
    }
}
